package com.example.yls.demoa;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*1.简单对话框*/
        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSimpleDialog();
            }
        });
        /*2.选择对话框*/
        findViewById(R.id.btn_dialog2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCheckDialog();
            }
        });
        /*3.单选对话框*/
        findViewById(R.id.btn_dialog3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRadioDialog();
            }
        });
        /*4.多选对话框*/
        findViewById(R.id.btn_dialog4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCheckBoxDialog();
            }
        });
        /*5.自定义对话框*/
        findViewById(R.id.btn_dialog5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCustomDialog();
            }
        });
    }

    /*5.自定义对话框*/
    private void showCustomDialog() {
        View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_dialog,null);
        final EditText username=(EditText)view.findViewById(R.id.username);
        final EditText password=(EditText)view.findViewById(R.id.password);

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请输入用户名和密码")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String a=username.getText().toString().trim();
                        String b=password.getText().toString().trim();
                        Toast.makeText(MainActivity.this,
                                "用户名:"+a+"  密码："+b,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .create()
                .show();
    }

    /*4.多选对话框*/
    private void showCheckBoxDialog() {
        final String[] hobbies={"足球","篮球","网球","桌球","跑步"};
        final boolean isChecked[]=new boolean[6];
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                    //checkedItems 默认选择
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        //保存每次点击后的值
                        isChecked[i]=b;
                        if(b){
                            Toast.makeText(MainActivity.this,
                                    "您添加了："+hobbies[i],Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,
                                    "您取消了："+hobbies[i],Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer=new StringBuffer();
                        for(int j=0;j<isChecked.length;j++){
                            if(isChecked[j]){
                                buffer.append(hobbies[j]);
                            }
                        }
                        Toast.makeText(MainActivity.this,"您的爱好有："+buffer,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .create()
                .show();
    }

    /*3.单选对话框*/
    private void showRadioDialog() {
        final String[] cities={"广州","上海","北京","香港","澳门"};
        final String[] sex={"男","女","未知性别"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                /*.setTitle("选择一个城市")*/
                .setTitle("请选择性别")
                /*.setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"您居住在："+cities[i],
                                Toast.LENGTH_SHORT).show();
                    }
                })*/
                .setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"您的性别是："+sex[i],
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .create()
                .show();
    }

    /*2.选择对话框*/
    /*which=-1就表示点击的是确定按钮，-2表示点击的是取消按钮，-3表示点击的是忽略按钮。*/
    private void showCheckDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("警告框")
                .setMessage("确定删除吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"positive: "+i,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"negative: "+i,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"neutral: "+i,
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    /*1.简单对话框*/
    private void showSimpleDialog() {
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher) // 对话框图标
                .setTitle("提示")  //对话框标题
                .setMessage("今天是你的生日")  //对话框内容
                /*1.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
                这是一个相当于OK、确定操作的按钮，
                2.setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
                这是一个相当于取消操作的按钮。
                3. setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
                这个是相当于一个忽略操作的按钮。*/
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {  //确定按钮
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create()
                .show();
    }
}
